package UIFree {
	import flash.events.Event;
	import flash.text.TextField;
	
	public class uif_fonts extends Object {
		private static var _fonts: Object 	= {};
		public static var app: uif_app		= null;
		public static var host: String 		= '';
		
		public static function applyFont(fontName: String, textField: TextField, 
									fontSize: Number, color: uint,
									doApplyProc: Function=null): void {
			loadFont(fontName, function(): void {
				updateFont(fontName, textField, fontSize, color);
				if (doApplyProc != null) doApplyProc();
			});
		}
		
		public static function loadFont(fontName: String, doLoad: Function=null): void {
			if (fontName == null) doLoad();
			else {
				if (!_fonts.hasOwnProperty(fontName)) {
					_fonts[fontName] = new uif_fontContent(app, host + 'fonts', fontName, function(e: Event): void {
						if (doLoad != null) doLoad();
					});
				} else {
					if (doLoad != null) {
						function doLLoad(e: Event): void {
							_fonts[fontName].contentLoaderInfo.removeEventListener(Event.COMPLETE, doLLoad);
							doLoad();
						}
						if (_fonts[fontName].loaded) doLoad();
						else _fonts[fontName].contentLoaderInfo.addEventListener(Event.COMPLETE, doLLoad);
					}
				}
			}
		}
		
		protected static function updateFont(fontName: String, textField: TextField, fontSize: Number, color: uint): void {
			_fonts[fontName].fontObject.applyFont(textField, fontSize, color);
		}
	}
}